package com.wkedu.repair.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wkedu.repair.dao.ReachMapper;
import com.wkedu.repair.dto.ReachDto;
import com.wkedu.repair.entity.Reach;
import com.wkedu.repair.service.ReachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 港港
 * @version 1.0
 * @date 2021/9/18 2:44 下午
 */
@Transactional
@Service
public class ReachServiceImpl implements ReachService {

    @Autowired
    private ReachMapper reachMapper;

    @Override
    public void add(ReachDto reachDto) {
        Reach reach = new Reach();
        reach.setReachDate(new Date());
        reach.setReachCourse(reachDto.getReachCourse());
        reach.setReachTeacher(reachDto.getReachTeacher());
        reach.setReachStudentId(reachDto.getReachStudentId());
        reachMapper.insert(reach);
    }

    @Override
    public List<ReachDto> listReach(String teacherName) {

        QueryWrapper<Reach> reachQueryWrapper = new QueryWrapper<>();
        reachQueryWrapper.eq("reach_teacher",teacherName);
        List<Reach> reaches = reachMapper.selectList(reachQueryWrapper);
        if ( !reaches.isEmpty() ) {
            List<ReachDto> reachDtoList = reaches.stream().map(i -> new ReachDto(
                    i.getReachStudentId(), i.getReachDate(),
                    i.getReachCourse(), i.getReachTeacher()
            )).collect(Collectors.toList());

            return reachDtoList;
        }else {
            return null;
        }

    }

    @Override
    public List<ReachDto> listReachForStudent(String studentId) {
        QueryWrapper<Reach> reachQueryWrapper = new QueryWrapper<>();
        reachQueryWrapper.eq("reach_student_id",studentId);

        List<Reach> reaches = reachMapper.selectList(reachQueryWrapper);

        if ( !reaches.isEmpty() ) {
            List<ReachDto> reachDtoList = reaches.stream().map(i -> new ReachDto(
                    i.getReachStudentId(), i.getReachDate(),
                    i.getReachCourse(), i.getReachTeacher()
            )).collect(Collectors.toList());

            return reachDtoList;
        }else {
            return null;
        }


    }
}
